package com.bridgeintelligent.tag.webserver.security;

import com.bridgeintelligent.tag.constants.ExceptionCodes;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy;
import org.apache.shiro.realm.Realm;

public class AtLeastOneSuccessfulStrategyPlus extends AtLeastOneSuccessfulStrategy {
    @Override
    public AuthenticationInfo afterAttempt(Realm realm, AuthenticationToken token, AuthenticationInfo singleRealmInfo, AuthenticationInfo aggregateInfo, Throwable t) throws AuthenticationException {
        if (t != null) {
            if (t instanceof AuthenticationException) {
                throw ((AuthenticationException) t);
            } else {
                StringBuilder msg = new StringBuilder()
                        .append("from realm [")
                        .append(realm)
                        .append("].  The [")
                        .append(getClass().getName())
                        .append(" implementation requires all configured realm(s) to operate successfully ")
                        .append("for a successful authentication.");
                throw new AuthenticationException(ExceptionCodes.SECURITY_50001.args(msg.toString()), t);
            }
        }
        return super.afterAttempt(realm, token, singleRealmInfo, aggregateInfo, t);
    }
}
